from django.shortcuts import render
from rest_framework import mixins
from rest_framework import generics
from rest_framework import filters
from rest_framework import permissions

from .models import *
from .serializers import *
from .permissions import *
from .filters import *

# Create your views here.
class BookList(mixins.ListModelMixin, mixins.CreateModelMixin, generics.GenericAPIView):

    queryset = Book.objects.all()
    serializer_class = BookSerializer
    permission_classes = (
        permissions.IsAuthenticated,
        IsSuperuserOrOwner
    )

    filter_backends = (
        filters.SearchFilter,
        filters.DjangoFilterBackend,
        filters.OrderingFilter,
        QuerysetFilter
    )
    search_fields = ('name', 'author', 'publisher')
    filter_fields = ('author', 'publisher')
    ordering_fields = ('id',)


    def get(self, request, *args, **kwargs):
        return self.list(request, *args, **kwargs)


    def post(self, request, *args, **kwargs):
        return self.create(request, *args, **kwargs)


class BookDetail(mixins.RetrieveModelMixin, mixins.UpdateModelMixin, mixins.DestroyModelMixin, generics.GenericAPIView):

    queryset = Book.objects.all()
    serializer_class = BookSerializer
    permission_classes = (
        permissions.IsAuthenticated,
        IsSuperuserOrOwner
    )

    filter_backends = (QuerysetFilter,)

    def get(self, request, *args, **kwargs):
        return self.retrieve(request, *args, **kwargs)

    def put(self, request, *args, **kwargs):
        return self.update(request, *args, **kwargs)

    def delete(self, request, *args, **kwargs):
        return self.destroy(request, *args, **kwargs)
